import { formatTime, toUrl } from "@/utils/common";
import { getUsersApi } from "@/api";
import { baiduSiteRestFulApi } from "@/api/baiduSite";
import mainStore from "@/stores/main";
import { Button, message, Popconfirm, Space, Tag, type TableColumnsType, Select, Modal } from "ant-design-vue";
import { levelOptions } from "./aiTask/common";
import { downloadFile, useAdminShow, useUserOptions } from "./useCommon";
const userOptions = useUserOptions()
export function useColumns() {
    const isReloadData = ref(false);
    const $store = mainStore();
    const columns: TableColumnsType = [
        {
            title: "任务名",
            dataIndex: "task_name",
            align: "center",
            width: 140
        },
        {
            title: "site域名",
            dataIndex: "target_domain",
            align: "center",
            width: 160,
            customRender: (opt) =>
                h("a", { onClick: () => window.open("https://" + opt.value, "_blank") }, { default: () => opt.value })
        },
        {
            title: "目标ES",
            dataIndex: "target_index",
            align: "center",
            sorter: true,
            width: 200,
            customRender: (opt) =>
                h(
                    "a",
                    {
                        onClick: () =>
                            window.open(
                                toUrl(`/esarticle/es-action?host=${opt.record.target_es}&index=${opt.record.target_index}`),
                                "_blank"
                            )
                    },
                    { default: () => opt.value }
                )
        },
        {
            title: "描述",
            dataIndex: "description",
            align: "center",
            width: 200
        },

        {
            title: "查询页数",
            dataIndex: "page_num",
            align: "center",
            width: 80
        },
        {
            title: "词总数",
            dataIndex: "total_num",
            align: "center",
            sorter: true,
            width: 80
        },
        {
            title: "ES文档数",
            dataIndex: "current_num",
            align: "center",
            sorter: true,
            width: 100
        },
        {
            title: "状态",
            dataIndex: "status",
            align: "center",
            width: 100,
            customRender: (opt) =>
                h(Tag, { color: opt.value === 1 ? "success" : "error" }, { default: () => (opt.value === 1 ? "完成" : "未完成") })
        },
        {
            title: "创建用户",
            dataIndex: "create_id",
            align: "center",
            width: 100,
            customRender: (opt) => userOptions.value.find((item) => item.value == opt.value)?.label
        },
        {
            title: "url key",
            dataIndex: "url_redis_key",
            align: "center",
            width: 220
        },
        {
            title: "keyword key",
            dataIndex: "keyword_redis_key",
            align: "center",
            width: 220
        },
        {
            title: "权重",
            dataIndex: "level",
            align: "center",
            width: 100,
            sorter: true
        },
        {
            title: "添加时间",
            dataIndex: "create_time",
            align: "center",
            sorter: true,
            width: 200,
            customRender: (opt) => formatTime(opt.value)
        },

        {
            title: "操作",
            align: "center",
            fixed: "right",
            width: 330,
            customRender: (row) => {
                const deleteBtn = h(
                    Popconfirm,
                    {
                        title: "确认删除吗",
                        okText: "确认",
                        cancelText: "取消",
                        onConfirm() {
                            baiduSiteRestFulApi({ ids: [row.record.id] }, "delete")
                                .then(() => {
                                    message.success("删除成功!");
                                    isReloadData.value = true;
                                    return;
                                })
                                .catch(() => message.error("删除失败!"));
                        }
                    },
                    {
                        default: () =>
                            h(
                                Button,
                                {
                                    danger: true,
                                    type: "link",
                                    style: {
                                        padding: "4px 8px"
                                    }
                                },
                                { default: () => "删除" }
                            )
                    }
                );
                const updateLevelBtn = h(
                    "a",
                    {
                        type: "text",
                        onClick() {
                            const level = ref(row.record.level);
                            Modal.info({
                                title: "修改权重",
                                icon: false,
                                closeIcon: true,
                                okCancel: true,
                                cancelText: "取消",
                                okText: "保存",
                                style: "width:260px",
                                onOk() {
                                    baiduSiteRestFulApi({ id: row.record.id, level: level.value }, "put")
                                        .then(() => {
                                            message.success("修改成功!");
                                            row.record.level = level.value;
                                        })
                                        .catch(() => message.error("修改失败!"));
                                },
                                content: () =>
                                    h(Select, {
                                        style: "width:100%",
                                        options: levelOptions,
                                        value: level.value,
                                        onSelect: (v: any) => (level.value = v)
                                    })
                            });
                        }
                    },
                    { default: () => "修改权重" }
                );
                const esQuery = h(
                    Button,
                    {
                        type: "link",
                        onClick: () => {
                            downloadFile("custom_field_map", row.record.es_query);
                        }
                    },
                    { default: () => "esQuery" }
                );
                const customFieldMap = h(
                    Button,
                    {
                        type: "link",
                        onClick: () => {
                            downloadFile("custom_field_map", row.record.custom_field_map);
                        }
                    },
                    { default: () => "自定义字段" }
                );
                const spaceChild = [esQuery, customFieldMap];
                if (useAdminShow()) {
                    spaceChild.push(updateLevelBtn);
                }
                if ($store.userInfo.id === row.value.uid || useAdminShow()) {
                    spaceChild.push(deleteBtn);
                }
                return h(Space, { size: 5 }, { default: () => spaceChild });
            }
        }
    ];
    return { columns, isReloadData, userOptions };
}
